package service

import (
	"context"
	"go-shop-api/pkg/e"
	"go-shop-api/pkg/serializer"
	"go-shop-api/pkg/zydhsms"
)

// SendSmsService 发送短信
type SendSmsService struct {
	Phone string `form:"phone" json:"phone" binding:"required,checkPhone"  _:"comment:手机号"`
	//验证码的key 和 dots
	Key  string `form:"key" json:"key" binding:"required" `
	Dots string `form:"dots" json:"dots"  binding:"required" `
	//go_captcha.CheckPostCaptcha
}

type CheckSmsService struct {
	Phone string `form:"phone" json:"phone" binding:"required,checkPhone"  _:"comment:手机号"`
	Code  string `form:"code" json:"code" binding:"required,numeric,len=6"  _:"comment:手机号"`
}

// SendSms 发送短信
func (ss *SendSmsService) SendSms(ctx context.Context) *serializer.Response {
	//检查验证码
	//res := go_captcha.CheckVerifyCode(ss.Key, ss.Dots)
	//if res.Status != e.Success {
	//	return res
	//}

	////检查手机号是否注册
	//userDao := dao.NewUserDao(ctx)
	////通过 账号/昵称/邮箱 判断用户是否存在
	////不存在
	//_, ok, err := userDao.ExistOrNotByAccount(ss.Phone)
	//if err != nil {
	//	return serializer.NewResponse(e.Error, err.Error(), e.TipsGeneral, nil)
	//}
	////不存在
	//if !ok {
	//	return serializer.NewResponse(e.ErrorExistUserNotFind, e.TipsNil, e.TipsNotExistUser, nil)
	//}
	////存在
	////检查 单位时间里发送短信的次数

	//发送短信
	//获取发送的6位验证码
	code := zydhsms.GetSmsCode(6)
	content := "您的验证码是：" + code + "。请不要把验证码泄漏给其他人。如非本人操作，可不用理会！【万奈庭】"
	//同步阻塞
	//resq := zydhsms.SendSms(ss.Phone, content)
	//if resq.Status == e.Success {
	//	//写入redis缓存
	//	//redis.SetKey("ss.Phone",encrypt(code))
	//	//写入sms发送日志表 - 看需求
	//	//writeSmsSendLog( scene:  phone  encrypt(code) )
	//}
	//return resq

	//开始go
	go zydhsms.SendSms(ss.Phone, content)
	//写入redis缓存 -- 移动到SendSms()方法中
	//写入sms发送日志表 - 看需求
	return serializer.NewResponse(e.Success, e.TipsOk, e.TipsSuccessSendSmsCode, nil)
}

// CheckSms 检查短信
func (cs *CheckSmsService) CheckSms(ctx context.Context) *serializer.Response {
	return zydhsms.CheckSms(cs.Phone, cs.Code)
}
